<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-20
 * Time: 上午9:49
 * 在线电桩
 */
namespace Factory\Controller;
use Mcit\Page;
class OnlineController extends BaseController {

    public function index(){

        //2---厂商  3---运营商
        if(session("fac_type") == 2){
            if (session('fac_key')){$condition_str="b.ev_fac_code='".session('fac_key')."' and c.yy_user_id='".session('uid')."'";
            }else{$condition_str="b.ev_fac_code='".session('fac_login_name')."'";}
        }elseif(session("fac_type") == 3){
            if(session('pid') != 0){
                $operation = "c.plant_attr=3 and c.yy_user_id='".session('uid')."'";
            }else{
                $yyuserid = M()->query("select id from ev_fac where id='".session('uid')."' OR pid='".session('uid')."'");
                foreach($yyuserid as $k=>$v){
                    $yyuserids .= $v['id'].",";
                }
                $yyuserids = trim($yyuserids,',');
                $operation = "c.plant_attr=3 and c.yy_user_id IN($yyuserids)";
            }
        }

        $evc_number = I("evc_number");
        $key_link   = I("key_link");
        if(!empty($_GET['evc_number'])){
            $sql = "select * from char_online a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                left join ev_plant c
                on c.id = b.ev_plant_id
                where {$condition_str} {$operation}
                and a.online = 1
                and UNIX_TIMESTAMP()-UNIX_TIMESTAMP(a.time_last) < 300
                and b.evc_number like '$evc_number%'";
        }elseif(!empty($_GET['key_link'])){
            $sql = "select * from char_online a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                left join ev_plant c
                on c.id = b.ev_plant_id
                where {$condition_str} {$operation}
                and a.online = 1
                and UNIX_TIMESTAMP()-UNIX_TIMESTAMP(a.time_last) < 300
                and a.key_link like '$key_link%'";
        }elseif(!empty($_GET['location_t_1'])){
            if(I('location_s_1') == 110100 || I('location_s_1') == 120100 || I('location_s_1') == 310100 || I('location_s_1') == 500100){
                if(I('location_s_1') == I('location_t_1')-1){
                    $location = I('location_f_1').",".I('location_s_1');
                }else{
                    $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
                }
            }else if(I('location_s_1') == I('location_t_1')-1){
                $location = I('location_f_1').",".I('location_s_1');
            }else{
                $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
            }
            $sql = "select * from char_online a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                left join ev_plant c
                on c.id = b.ev_plant_id
                where {$condition_str} {$operation}
                and a.online = 1
                and UNIX_TIMESTAMP()-UNIX_TIMESTAMP(a.time_last) < 300
                and c.location like '$location%'";
        }else{
            $sql = "select * from char_online a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                left join ev_plant c
                on c.id = b.ev_plant_id
                where {$condition_str} {$operation}
                and a.online = 1
                and UNIX_TIMESTAMP()-UNIX_TIMESTAMP(a.time_last) < 300";
        }
        $count = count(M()->query($sql));
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
//        $nowPage = isset($_GET['page']) ? $_GET['page'] * 20 - 20 : 0;
        if($_GET['page'] == 0){
            $nowPage = isset($_GET['page']) ? $_GET['page'] : 0;
        }else{
            $nowPage = isset($_GET['page']) ? $_GET['page'] * 20 - 20 : 0;
        }
        if(!empty($_GET['evc_number'])){
            $sql1 = "select a.id,b.evc_number,a.key_link,c.name,c.location,b.evc_type,b.evc_quantity,b.card_wifi,b.sys_creat_time from char_online a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                left join ev_plant c
                on c.id = b.ev_plant_id
                where {$condition_str} {$operation}
                and a.online = 1
                and UNIX_TIMESTAMP()-UNIX_TIMESTAMP(a.time_last) < 300
                and b.evc_number like '$evc_number%'
                limit $nowPage,".$Page->listRows;
        }elseif(!empty($_GET['key_link'])){
            $sql1 = "select a.id,b.evc_number,a.key_link,c.name,c.location,b.evc_type,b.evc_quantity,b.card_wifi,b.sys_creat_time from char_online a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                left join ev_plant c
                on c.id = b.ev_plant_id
                where {$condition_str} {$operation}
                and a.online = 1
                and UNIX_TIMESTAMP()-UNIX_TIMESTAMP(a.time_last) < 300
                and a.key_link like '$key_link%'
                limit $nowPage,".$Page->listRows;
        }elseif(!empty($_GET['location_t_1'])){
            if(I('location_s_1') == 110100 || I('location_s_1') == 120100 || I('location_s_1') == 310100 || I('location_s_1') == 500100){
                if(I('location_s_1') == I('location_t_1')-1){
                    $location = I('location_f_1').",".I('location_s_1');
                }else{
                    $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
                }
            }else if(I('location_s_1') == I('location_t_1')-1){
                $location = I('location_f_1').",".I('location_s_1');
            }else{
                $location = I('location_f_1').",".I('location_s_1').",".I('location_t_1');
            }
            $sql1 = "select a.id,b.evc_number,a.key_link,c.name,c.location,b.evc_type,b.evc_quantity,b.card_wifi,b.sys_creat_time from char_online a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                left join ev_plant c
                on c.id = b.ev_plant_id
                where {$condition_str} {$operation}
                and a.online = 1
                and UNIX_TIMESTAMP()-UNIX_TIMESTAMP(a.time_last) < 300
                and c.location like '$location%'
                limit $nowPage,".$Page->listRows;
        }else{
            $sql1 = "select a.id,b.evc_number,a.key_link,c.name,c.location,b.evc_type,b.evc_quantity,b.card_wifi,b.sys_creat_time from char_online a
                LEFT JOIN ev_char b
                on b.key_link = a.key_link
                left join ev_plant c
                on c.id = b.ev_plant_id
                where {$condition_str} {$operation}
                and a.online = 1
                and UNIX_TIMESTAMP()-UNIX_TIMESTAMP(a.time_last) < 300
                limit $nowPage,".$Page->listRows;
        }
        $ev_char = M()->query($sql1);
//        P($ev_char);
        //echo D("Char")->getLastSql();
        //分页结束
        foreach($ev_char as $k=>$v){
            $region_arr = strsToArray($v['location']);//查所在地
            $region = D("Admin/Region")->where("code='".$region_arr[2]."'")->getField('fullname');
            $ev_char[$k]['plant_name'] = $v['name'];//站点名称
            if(mb_strlen($v['name'],'utf-8') <= 17){
                $ev_char[$k]['plant_name_text'] = $v['name'];
            }else{
                $ev_char[$k]['plant_name_text'] = mb_substr($v['name'],0,17,"utf-8")."···";
            }
            $ev_char[$k]['region_text'] = $region;
        }
        $this->china = D("Admin/Region")->getChinaArea();
        //P($ev_char);
        $this->assign('ev_char',$ev_char);
        $this->display();
    }
    //查看电桩详细信息
    public function detail(){
        if(IS_GET){

            $data   = D("Admin/Online")->where("id='".$_GET['id']."'")->find();
            $char   = M("char")->where("key_link=".$data['key_link'])->find();
            $plant  = M("plant")->where("id=".$char['ev_plant_id'])->find();
            $region = strsToArray($plant['location']);
            $region_text  = D("Region")->where("code='".$region[2]."'")->getField("fullname");
            $data['name'] = $plant['name'];
            $data['location_text'] = $region_text;
            $trademark = M("fac")->where("fac_login_name=".$char['ev_fac_code'])->find();
            $fac_type1 = M("device_type")->where("id=".$char['ev_fac_type1'])->find();
            $fac_type2 = M("device_type")->where("id=".$char['ev_fac_type2'])->find();
            $data['trademark']     = $trademark['name'];
            $data['ev_fac_code']   = $trademark['fac_name'];
            $data['ev_fac_type1']  = $fac_type1['name'];
            $data['ev_fac_type2']  = $fac_type2['name'];
            $data['evc_sn']        = $char['evc_sn'];
            $data['evc_quantity']  = $char['evc_quantity'];
            $data['card_wifi']     = $char['card_wifi'];
            $data['s_link']        = $char['s_link'];
            $data['h_link']        = $char['h_link'];
            $data['h_link_port']   = $char['h_link_port'];
            $data['evc_number']    = $char['evc_number'];
            $data['sys_creat_time'] = $char['sys_creat_time'];
            $this->data = $data;
        }
        $this->display();
    }
}